package com.zzl.mine.util;

import java.math.BigDecimal;

import org.apache.commons.lang3.StringUtils;

public class MoneyUtils {

	
	public static String fen2yuan(Integer total_fee){
		BigDecimal total_fee_bd = new BigDecimal(total_fee);
	 	String total_cost = total_fee_bd.divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP).toPlainString();
	 	return total_cost;
	}

	public static int yuan2fen(String money){
	 	int total_cost = new BigDecimal(money).multiply(new BigDecimal(100)).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
	 	return total_cost;
	}

	
	public static int toInt(String money){
		if(StringUtils.isEmpty(money)){
			return 0;
		}
		BigDecimal money_bd = new BigDecimal(money);
	 	return money_bd.intValue();
	}
	
    //金额验证  
    public static boolean isMoney(String str){   
         java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$"); // 判断小数点后2位的数字的正则表达式  
         java.util.regex.Matcher match = pattern.matcher(str);   
         if(match.matches()==false){   
            return false;   
         }else{   
            return true;   
         }   
    }
    
    /**
     * 格式化为两位小数
     * */
    public static String formatTwoDecimalPlaces(String money){
    	//String f = String.format("%.2f", Double.valueOf(money)).toString();
		java.text.DecimalFormat df = new java.text.DecimalFormat("0.00");
		df.setRoundingMode(java.math.RoundingMode.HALF_UP);
		String f = df.format(Double.valueOf(money));
    	return f;
    }
    
	public static void main(String[] args) {
		//System.out.println(String.format("%012d",1));
		System.out.println(isMoney("499.999"));
	}
}
